<template>
	<view>
		<view class="container">
			<!-- <view class="password-cell">
				<text>手机号码</text> <input type="number" class="yzm-input" v-model="phoneNumber" maxlength="11" placeholder="请输入注册时的手机号"
				 placeholder-style="color:#a6a6a6; font-size:18rpx;" />
			</view> -->
			<view class="password-cell">
				<text>旧密码</text> <input type="text" placeholder="请输入旧密码" v-model="oldPassword" class="yzm-input" placeholder-style="color:#a6a6a6; font-size:18rpx;" />
			</view>
			<view class="password-cell">
				<text>新密码</text> <input type="text" placeholder="请设置新登录密码" v-model="newPassword" class="yzm-input"
				 placeholder-style="color:#a6a6a6; font-size:18rpx;" />
			</view>
			
			<view class="password-cell" v-if="ishongmeng">
				<u-button  type="primary" @click="submit">保存</u-button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				seconds: 60,
				codeBtn: {
					text: '获取验证码',
					waitingCode: false,
					count: this.seconds,
					vtoken: ''
				},
				vcodeNumber: '',
				phoneNumber: '',
				oldPassword: '',
				newPassword: '',
				ishongmeng:false,
			}
		},
		onNavigationBarButtonTap(e) {
			this.submit();
		},
		onShow() {
			//this.getSendCodeVToken();
			this.checkLogin(1);
			// console.log(1,uni.getSystemInfoSync().platform)
			if(uni.getSystemInfoSync().platform === 'harmonyos'){
				this.ishongmeng=true;
			}
		},
		onLoad() {
			
			this.checkLogin(1);
		},
		computed: {
			disableCodeBtn: function() {
				return this.codeBtn.waitingCode;
			}
		},
		methods: {

			sendCode() {
				//等待验证码
				if (this.codeBtn.waitingCode)
					return false;
				//如果初始验证密钥为空，重新获取
				if (this.phoneNumber != "" && this.codeBtn.vtoken == "") {
					this.getSendCodeVToken();
				}
				//验证手机号码
				if (!(/^1\d{10}$/.test(this.phoneNumber))) {
					uni.showToast({
						title: '请填写正确手机号码',
						icon: "none"
					});
					return false;
				} else if (this.codeBtn.vtoken == "") {
					uni.showToast({
						title: '当前页面已失效，请重新打开本页面！',
						icon: "none"
					});
					return false;
				}
				this.codeBtn.waitingCode = true;
				this.codeBtn.count = this.seconds;
				this.codeBtn.text = this.codeBtn.count + '秒后重发';

				let countdown = setInterval(() => {
					this.codeBtn.count--;
					this.codeBtn.text = this.codeBtn.count + '秒后重发';
					if (this.codeBtn.count < 0) {
						clearInterval(countdown);
						this.codeBtn.text = '重新发送';
						this.codeBtn.waitingCode = false;
					}
				}, 1000);
				//访问接口获取验证码
				let data = this.initNewData();
				//位置类别
				data.location = 5197;
				data.vtoken = this.codeBtn.vtoken;
				data.mobile = this.phoneNumber;
				uni.request({
					url: this.baseConfig.serverURL + 'sms/GetVCodeHandler.ashx', //检查更新的服务器地址
					data: data,
					method: "POST",
					header: {
						"content-type": "application/x-www-form-urlencoded" //"application/json"
					},
					success: (res) => {
						if (res.statusCode == 200) {
							if (!res.data.ret) {
								uni.showToast({
									title: res.data.msg,
									icon: "none"
								});
							}
						} else {
							uni.showToast({
								title: '获取验证码失败',
								icon: "none"
							});
						}
					},
					fail: (res) => {
						uni.showToast({
							title: '获取验证码失败，可能已超时！',
							icon: "none"
						});
					}
				})
			},
			submit() {
				//验证手机号码
				// if (!(/^1\d{10}$/.test(this.phoneNumber))) {
				// 	uni.showToast({
				// 		title: '请填写正确手机号码',
				// 		icon: "none"
				// 	});
				// 	return false;
				// }
				if (this.oldPassword.trim() == '') {
					uni.showToast({
						title: '旧密码不能为空',
						icon: "none"
					});
					return false;
				}
				if (this.newPassword.trim() == '') {
					uni.showToast({
						title: '新密码不能为空',
						icon: "none"
					});
					return false;
				}
				let that = this;
				let data = this.initNewData();
				data.location = 5197;
				//data.tel = this.phoneNumber;
				data.newPass = this.newPassword;
				data.oldPass = this.oldPassword;
				uni.request({
					url: this.baseConfig.serverURL + 'user/setting/ChangePasswordHandler.ashx', //检查更新的服务器地址
					data: data,
					method: "POST",
					header: {
						"content-type": "application/x-www-form-urlencoded" //"application/json"
					},
					success: (result) => {
						if (result.statusCode == 200) {
							if (!result.data.ret) {
								uni.showToast({
									title: result.data.msg,
									icon: "none"
								});
							} else {
								//成功，绑定
								uni.showToast({
									title: result.data.msg,
									icon: "none",
									success: function() {
										setTimeout(function() {
											that.doLogout()
											uni.switchTab({
												url: '/pages/user/user'
											})
										}, 2000)
									}
								});
							}
						} else {
							uni.showToast({
								title: '修改失败,请重试！',
								icon: "none"
							});
						}
					},
					fail: (e) => {
						uni.showToast({
							title: '修改失败,请重试！',
							icon: "none"
						});
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	@import '../../../common/common.css';

	.password-cell {
		display: flex;
		flex-direction: row;
		align-items: center;
		margin-left: 30rpx;
		padding: 20rpx 0;
		border-bottom: 1rpx solid #dfdddd;

		text {
			font-size: 32rpx;
			width: 200rpx;
		}

		.yzm-input {
			flex: 1 1 0%;
			font-size: 28rpx;
		}
	}

	.password-cell:last-child {
		border-bottom: 0;
	}

	.yzm-btn {
		background-color: #fff;
		border: 0;
		color: #2c6bc5;
		font-size: 28rpx;
		line-height: inherit;
	}

	.yzm-btn::after {
		border: 0;
	}
</style>
